package com.it.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.it.enums.ErrorCode;
import com.it.common.ResultResponse;
import com.it.common.ResultUtils;
import com.it.exception.BusinessException;
import com.it.model.entity.User;
import com.it.model.request.ResetPasswordRequest;
import com.it.model.request.UserLoginRequest;
import com.it.model.request.UserRegisterRequest;
import com.it.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 帅宏-coding @Money java_offer_13k
 * @date 2025/4/16 星期三
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户User相关接口")
// @CrossOrigin(origins = "http://localhost:5000", allowCredentials = "true")
public class UserController {

  @Resource private UserService userService;

  /**
   * 用户注册
   *
   * @param userRegisterRequest
   * @return
   */
  @ApiOperation(value = "用户注册")
  @PostMapping("/register")
  public ResultResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
    // 校验
    if (userRegisterRequest == null) {
      throw new BusinessException(ErrorCode.NULL_ERROR, "请求的参数userRegisterRequest为空...");
    }
    String loginAccount = userRegisterRequest.getLoginAccount();
    String loginPassword = userRegisterRequest.getLoginPassword();
    String checkPassword = userRegisterRequest.getCheckPassword();

    Long result = userService.userRegister(loginAccount, loginPassword, checkPassword);
    return ResultUtils.success(result);
  }

  /**
   * 用户登录
   *
   * @param userLoginRequest
   * @param request
   * @return
   */
  @PostMapping("/login")
  @ApiOperation(value = "用户登录")
  public ResultResponse<User> userLogin(
      @RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
    // 校验
    if (userLoginRequest == null) {
      throw new BusinessException(ErrorCode.NULL_ERROR, "请求的参数userLoginRequest为空...");
    }
    String loginAccount = userLoginRequest.getLoginAccount();
    String loginPassword = userLoginRequest.getLoginPassword();

    User user = userService.userLogin(loginAccount, loginPassword, request);
    return ResultUtils.success(user);
  }

  /**
   * 用户退出
   *
   * @param request
   * @return
   */
  @PostMapping("/logout")
  @ApiOperation(value = "用户退出")
  public ResultResponse<Integer> userLogout(HttpServletRequest request) {
    // 校验
    if (request == null) {
      throw new BusinessException(ErrorCode.NULL_ERROR, "请求的参数userLoginRequest为空...");
    }
    Integer result = userService.userLogout(request);
    return ResultUtils.success(result);
  }

  /**
   * 管理员根据昵称模糊查询账号人员信息
   *
   * @param username
   * @param request
   * @return
   */
  @PostMapping("/search")
  @ApiOperation(value = "管理员根据名字查询信息")
  public ResultResponse<List<User>> getAllUsersByName(
      @RequestParam("username") String username, HttpServletRequest request) {
    if (StrUtil.isBlank(username)) {
      throw new BusinessException(ErrorCode.NULL_ERROR, "请求参数username为空...");
    }
    List<User> userList = userService.searchUsers(username, request);
    return ResultUtils.success(userList);
  }

  /**
   * 更新用户信息
   *
   * @param user
   * @param request
   * @return
   */
  @PostMapping("/update")
  @ApiOperation(value = "用户修改个人信息")
  public ResultResponse<Integer> updateUser(@RequestBody User user, HttpServletRequest request) {
    // 校验参数是否为空
    if (user == null) {
      throw new BusinessException(ErrorCode.NULL_ERROR, "传入要修改的用户数据为空");
    }
    User loginUser = userService.getCurrentLoginUser(request);
    int result = userService.updateUser(user, loginUser);
    return ResultUtils.success(result);
  }

  /**
   * 获取用户标签
   *
   * @param request 请求
   * @return {@link ResultResponse}<{@link List}<{@link String}>>
   */
  @GetMapping("/tags")
  @ApiOperation(value = "获取当前用户标签")
  @ApiImplicitParams({@ApiImplicitParam(name = "request", value = "request请求")})
  public ResultResponse<List<String>> getUserTags(HttpServletRequest request) {
    User loginUser = userService.getCurrentLoginUser(request);
    if (loginUser == null) {
      throw new BusinessException(ErrorCode.NOT_LOGIN);
    }
    List<String> userTags = userService.getUserTags(loginUser.getId());
    return ResultUtils.success(userTags);
  }

  /**
   * 更新用户标签
   *
   * @param tags 标签
   * @param request 请求
   * @return {@link ResultResponse}<{@link String}>
   */
  @PutMapping("/update/tags")
  @ApiOperation(value = "更新用户标签")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "tags", value = "标签"),
    @ApiImplicitParam(name = "request", value = "request请求")
  })
  public ResultResponse<String> updateUserTags(
      @RequestBody List<String> tags, HttpServletRequest request) {
    User loginUser = userService.getCurrentLoginUser(request);
    if (loginUser == null) {
      throw new BusinessException(ErrorCode.NOT_LOGIN);
    }
    userService.updateTags(tags, loginUser.getId());
    return ResultUtils.success("ok");
  }

  /**
   * 根据标签去查询数据
   *
   * @param tagNameList
   * @return
   */
  @GetMapping("/search/tags")
  @ApiOperation(value = "根据用户标签去查询数据")
  public ResultResponse<List<User>> searchAllUsersByTags(@RequestParam List<String> tagNameList) {
    if (CollUtil.isEmpty(tagNameList)) {
      throw new BusinessException(ErrorCode.NULL_ERROR, "请求传入的标签集合为空...");
    }
    List<User> resultList = userService.searchUsersByTags(tagNameList);
    return ResultUtils.success(resultList);
  }

  //  * @RequestParam
  //  * @PathVariable
  //  * @RequestBody

  @GetMapping("/recommend")
  @ApiOperation(value = "推荐主页")
  public ResultResponse<List<User>> recommendUsers(
      @RequestParam("pageNum") Long pageNum,
      @RequestParam("pageSize") Long pageSize,
      HttpServletRequest request) {
    if (pageNum == null || pageSize == null) {
      throw new BusinessException(ErrorCode.NULL_ERROR, "请求参数页码为空...");
    }

    List<User> userList = userService.recommendUsers(pageNum, pageSize, request);
    return ResultUtils.success(userList);
  }

  /**
   * 获取当前用户
   *
   * @param request
   * @return
   */
  @GetMapping("/current")
  @ApiOperation(value = "获取当前的登录用户信息")
  public ResultResponse<User> getCurrentLoginUser(HttpServletRequest request) {
    User currentLoginUser = userService.getCurrentLoginUser(request);
    return ResultUtils.success(currentLoginUser);
  }

  /**
   * 根据id进行逻辑删除
   *
   * @param userId
   * @param request
   * @return
   */
  @DeleteMapping("/delete")
  @ApiOperation(value = "根据id进行逻辑删除")
  public ResultResponse<Boolean> deleteUserById(
      @RequestParam("userId") Integer userId, HttpServletRequest request) {
    if (userId == null) throw new BusinessException(ErrorCode.NULL_ERROR, "请求参数userId为空...");
    boolean result = userService.deleteUserById(userId, request);
    return ResultUtils.success(result);
  }

  /**
   * 获取最匹配的用户
   *
   * @param num
   * @param request
   * @return
   */
  @GetMapping("/match")
  @ApiOperation(value = "心动模式: 逻辑匹配算法匹配标签")
  public ResultResponse<List<User>> matchUsers(long num, HttpServletRequest request) {
    if (num <= 0 || num > 20) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }
    User user = userService.getCurrentLoginUser(request);
    return ResultUtils.success(userService.matchUsers(num, user));
  }

  //    resetPassword

  /**
   * 修改密码
   *
   * @param resetPasswordRequest
   * @param request
   * @return
   */
  @ApiOperation(value = "修改密码")
  @PostMapping("/resetPassword")
  public ResultResponse<Integer> resetPassword(
      @RequestBody ResetPasswordRequest resetPasswordRequest, HttpServletRequest request) {
    String oldPassword = resetPasswordRequest.getOldPassword();
    String newPassword = resetPasswordRequest.getNewPassword();
    String newCheckPassword = resetPasswordRequest.getNewCheckPassword();
    if (StrUtil.isEmpty(oldPassword)) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "旧密码输入不能为空~~~");
    }
    if (StrUtil.isEmpty(newPassword)) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "新密码输入不能为空~~~");
    }
    if (StrUtil.isEmpty(newCheckPassword)) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "确认密码输入不能为空~~~");
    }
    User loginUser = userService.getCurrentLoginUser(request);
    Integer result =
        userService.resetPassword(oldPassword, newPassword, newCheckPassword, loginUser);
    return ResultUtils.success(result);
  }

  @GetMapping("/test")
  public String testString() {
    return "成功!....";
  }
}
